﻿// VerticalSpeedIndicator - 垂直速度表

#include "VerticalSpeedIndicator.h"

static int r = 250; //半径
static int X = 4 * r;//窗口长
static int Y = 3 * r; //窗口宽
static int x = 2 * r;//盘心的横坐标
static int y = 8 * r / 5; //盘心的纵坐标

void drawVS(int x0, int y0, int r0, double VerticalSpeed)
{
	x = x0;
	y = y0;
	r = r0;
	//setinitmode(0, r * 8 / 5, r / 5 * 4);//(0,x,y)
	//setcaption("垂直速度表");//窗口名称
	//initgraph(X, Y, INIT_RENDERMANUAL);
	//setrendermode(RENDER_MANUAL);
	//setbkcolor(WHITE);
	ege_enable_aa(true); //开启抗锯齿
	settextjustify(LEFT_TEXT, TOP_TEXT);

	if (VerticalSpeed > 2000)
		VerticalSpeed = 2000;
	if (VerticalSpeed < -2000)
		VerticalSpeed = -2000;
	double angle = VerticalSpeed/100*60/7;
	double radin = angle * PI / 180 - PI;
	VSscrew(x0, y0, r0 * 1.03);
	//for (; is_run(); delay_fps(60)) {
	//	cleardevice();
		draw_frameworkVS();
		draw_pointerVS(radin);
	//	if (kbhit()) { // 检测是否有按键被按下
	//		int key = getch(); // 获取按下的键值
	//		switch (key) {
	//		case 'W': // 上箭头键
	//			radin += 0.8 * PI / 180; // 增加角度
	//			break;
	//		case 'S': // 下箭头键
	//			radin -= 0.8 * PI / 180; // 减少角度
	//			break;
	//		case 'A': // 左箭头键
	//			radin -= 0.8 * PI / 180; // 逆时针旋转
	//			break;
	//		case 'D': // 右箭头键
	//			radin += 0.8 * PI / 180; // 顺时针旋转
	//			break;
	//		case 27:
	//			return 0;
	//		}
	//	}
	//	//getch();
	//	if (radin > 2 * PI) radin -= 2 * PI;
	//	if (radin < 0) radin += 2 * PI;
	//}
	//closegraph();
	//return 0;
}

void draw_kuangVS() {

	int circle_r = r * 1;
	int circle_x = x;
	int circle_y = y;

	setcolor(BLACK);//描边颜色
	setlinewidth(15 * r / 250.0);//线条粗细
	ege_circle(circle_x, circle_y, circle_r + circle_r / 10);//描边

	ege_point p5 = { circle_x - circle_r - circle_r / 10 + circle_r / 30 * 11 ,circle_y - circle_r - circle_r / 10 + circle_r / 30 * 11 };
	ege_setpattern_ellipsegradient(p5, EGERGB(160, 160, 160), circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10), EGEACOLOR(0xFF, 0x00161616));
	ege_fillrect(circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10));

	//最外圈


	setcolor(BLACK);//描边颜色
	setlinewidth(2 * r / 250.0);//线条粗细
	ege_circle(circle_x, circle_y, circle_r + circle_r * 3 / 150);//描边

	ege_point p6 = { circle_x - circle_r - circle_r / 300 * 2 + circle_r / 30 * 9 ,circle_y - circle_r - circle_r / 300 * 2 + circle_r / 30 * 9 };
	ege_setpattern_ellipsegradient(p6, EGERGB(200, 200, 200), circle_x - circle_r - circle_r / 300 * 6, circle_y - circle_r - circle_r / 300 * 6, 2 * (circle_r + circle_r / 300 * 6), 2 * (circle_r + circle_r / 300 * 6), EGEACOLOR(0xFF, 0x003A3A3C));
	ege_fillrect(circle_x - circle_r - circle_r / 300 * 6, circle_y - circle_r - circle_r / 300 * 6, 2 * (circle_r + circle_r / 300 * 6), 2 * (circle_r + circle_r / 300 * 6));
	//第二圈


	setlinewidth(14 * r / 250.0);//线条粗细
	setfillcolor(EGERGB(50, 50, 50));//填充为灰色
	ege_circle(circle_x, circle_y, circle_r - circle_r / 30);//描边

	ege_point p7 = { circle_x  ,circle_y };
	ege_setpattern_ellipsegradient(p7, EGERGB(70, 70, 70), circle_x - circle_r + circle_r / 30, circle_y - circle_r + circle_r / 30, 2 * (circle_r - circle_r / 30), 2 * (circle_r - circle_r / 30), EGEACOLOR(0xFF, 0x00292925));
	ege_fillrect(circle_x - circle_r + circle_r / 30, circle_y - circle_r + circle_r / 30, 2 * (circle_r - circle_r / 30), 2 * (circle_r - circle_r / 30));


	//ege_fillcircle(circle_x, circle_y, circle_r - circle_r / 30);
	//setfillcolor(EGERGB(38, 38, 38));//填充为深灰色
//第三圈
}

void draw_frameworkVS() {
	draw_kuangVS();

	setcolor(EGEARGB(42, 42, 42, 42));
	setfillcolor(EGEARGB(42, 42, 42, 42));//填充的颜色

	//ege_rectangle(x - r * 2, y - r * 2, x + r * 2, y + 2 * r);
	//ege_fillrect(x - r * 2, y - r * 2, x + r * 2, y + 2 * r);

	//ege_circle(x, y, r*0.9);
	//fillcircle(x, y, r*0.9);//填充圆
	//  设置颜色 颜色的类型：color_t  分RGB和ARGB两种 

	setcolor(WHITE);//刻度颜色
	//绘制刻度
	  // 绘制刻度
	int numTicks = 42; // 刻度数量
	double angleStep = 360.0 / numTicks; // 每个刻度之间的角度
	double startAngle = 180; // 起始角度
	//画左上半圈
	for (int i = 1; i < (numTicks - 2) / 4 + 1; i++)
	{
		if (i % 5 == 0) continue;
		double angle = startAngle + i * angleStep; // 当前刻度的角度
		double radian = angle * PI / 180.0;     // 角度转弧度

		// 计算刻度线的起点和终点
		double startX = x + (r * 0.77 * cos(radian));
		double startY = y + (r * 0.77 * sin(radian));
		double endX = x + (r * 0.95 * cos(radian));
		double endY = y + (r * 0.95 * sin(radian));

		// 绘制刻度线
		setlinewidth(3 * r / 250.0);
		ege_line(startX, startY, endX, endY);
	}

	//画左下半圈
	numTicks = 42; // 刻度数量
	angleStep = 360.0 / numTicks; // 每个刻度之间的角度
	startAngle = 180; // 起始角度
	for (int i = 1; i < (numTicks - 2) / 4 + 1; i++)
	{
		if (i % 5 == 0) continue;
		double angle = startAngle - i * angleStep; // 当前刻度的角度
		double radian = angle * PI / 180.0;     // 角度转弧度

		// 计算刻度线的起点和终点
		double startX = x + (r * 0.77 * cos(radian));
		double startY = y + (r * 0.77 * sin(radian));
		double endX = x + (r * 0.95 * cos(radian));
		double endY = y + (r * 0.95 * sin(radian));

		// 绘制刻度线
		setlinewidth(3 * r / 250.0);
		ege_line(startX, startY, endX, endY);
	}

	//绘制刻度的变量
	setbkmode(TRANSPARENT);//透明字体背景
	setfont(r / 5, 0, "Arial");//字体

	//实现大刻度的绘制
	numTicks = 42; // 刻度数量
	angleStep = 360.0 / numTicks * 5; // 每个刻度之间的角度
	startAngle = 180; // 起始角度
	for (int i = 0; i < (numTicks - 2) / 8; i++)
	{
		double angle = startAngle + i * angleStep; // 当前刻度的角度
		double radian = angle * PI / 180.0;     // 角度转弧度

		// 计算刻度线的起点和终点
		double startX = x + (r * 0.71 * cos(radian));
		double startY = y + (r * 0.71 * sin(radian));
		double endX = x + (r * 0.95 * cos(radian));
		double endY = y + (r * 0.95 * sin(radian));

		// 绘制刻度线
		setlinewidth(6 * r / 250.0);
		ege_line(startX, startY, endX, endY);
	}
	//绘制数字
	settextcolor(WHITE);
	ege_outtextxy(x + (r * 0.71 * cos(PI)) + r / 40, y + (r * 0.8 * sin(PI)) - r / 10, "0");
	ege_outtextxy(x + (r * 0.68 * cos(PI + angleStep * PI / 180)), y + (r * 0.79 * sin(PI + angleStep * PI / 180)), "5");
	ege_outtextxy(x + (r * 0.73 * cos(PI + angleStep * 2 * PI / 180)) - r / 10, y + (r * 0.70 * sin(PI + angleStep * 2 * PI / 180)), "10");
	ege_outtextxy(x + (r * 0.73 * cos(PI + angleStep * 3 * PI / 180)) - r / 5, y + (r * 0.73 * sin(PI + angleStep * 3 * PI / 180)), "15");

	numTicks = 42; // 刻度数量
	angleStep = 360.0 / numTicks * 5; // 每个刻度之间的角度
	startAngle = 180; // 起始角度
	for (int i = 0; i < (numTicks - 2) / 8; i++)
	{
		double angle = startAngle - i * angleStep; // 当前刻度的角度
		double radian = angle * PI / 180.0;     // 角度转弧度

		// 计算刻度线的起点和终点
		double startX = x + (r * 0.71 * cos(radian));
		double startY = y + (r * 0.71 * sin(radian));
		double endX = x + (r * 0.95 * cos(radian));
		double endY = y + (r * 0.95 * sin(radian));

		// 绘制刻度线
		setlinewidth(6 * r / 250.0);
		ege_line(startX, startY, endX, endY);

	}
	//数字
	ege_outtextxy(x + (r * 0.71 * cos(PI)) + r / 40, y + (r * 0.8 * sin(PI)) - r / 10, "0");
	ege_outtextxy(x + (r * 0.68 * cos(PI - angleStep * PI / 180)), y + (r * 0.79 * sin(PI - angleStep * PI / 180)) - r / 7, "5");
	ege_outtextxy(x + (r * 0.73 * cos(PI - angleStep * 2 * PI / 180)) - r / 10, y + (r * 0.70 * sin(PI - angleStep * 2 * PI / 180)) - r / 6, "10");
	ege_outtextxy(x + (r * 0.73 * cos(PI - angleStep * 3 * PI / 180)) - r / 5, y + (r * 0.73 * sin(PI - angleStep * 3 * PI / 180)) - r / 6, "15");

	ege_outtextxy(x + (r * 0.73 * cos(PI - angleStep * 4 * PI / 180)) - r / 5, y + (r * 0.73 * sin(PI - angleStep * 4 * PI / 180)) - r / 5, "20");
	//绘制字符
	setfont(r / 10, 0, "Arial");
	//右上
	ege_outtextxy(x + (r * 0.73 * cos(PI + angleStep * 3 * PI / 180)) - r / 2.6, y + (r * 0.68 * sin(PI + angleStep * 3 * PI / 180)) + r / 4.8, "VERTICAL");
	ege_outtextxy(x + (r * 0.73 * cos(PI + angleStep * 3 * PI / 180)) - r / 2.6, y + (r * 0.73 * sin(PI + angleStep * 3 * PI / 180)) + r / 2.9, "SPEED");
	//右下
	setfont(r / 11, 0, "Arial");
	ege_outtextxy(x + (r * 0.70 * cos(PI + angleStep * 3 * PI / 180)) - r / 2.6, y + (r * 0.54 * sin(PI + angleStep * 3 * PI / 180)) + r / 1.5, "100 FEET");
	ege_outtextxy(x + (r * 0.72 * cos(PI + angleStep * 3 * PI / 180)) - r / 2.6, y + (r * 0.6 * sin(PI + angleStep * 3 * PI / 180)) + r / 1.25, "PER MIN");
	//左上
	ege_outtextxy(x + (r * 0.55 * cos(PI - angleStep * PI / 180)), y + (r * 0.73 * sin(PI - angleStep * PI / 180)) - r / 1.18, "UP");
	//左下
	ege_outtextxy(x + (r * 0.55 * cos(PI - angleStep * PI / 180)), y + (r * 0.82 * sin(PI - angleStep * PI / 180)) - r / 4, "DN");
	//开始绘制
	setfillcolor(GREEN);
	//左上的指示
	ege_point polypoints1[6] = { x - 120 * r / 250 , y - 13 * r / 250,x - 140 * r / 250,y - 25 * r / 250,x - 110 * r / 250, y - 83 * r / 250 };
	ege_fillpoly(3, polypoints1);
	//右上的指示
	setfillcolor(RED);
	ege_point polypoints2[6] = { x - 120 * r / 250,y + 8 * r / 250,x - 140 * r / 250,y + 20 * r / 250,x - 110 * r / 250,y + 78 * r / 250 };
	ege_fillpoly(3, polypoints2);

}

void draw_pointerVS(double radin) {
	setfillcolor(WHITE);
	ege_circle(x, y, r / 25);
	ege_fillcircle(x, y, r / 25);
	ege_point polypoint[] = { x + r / 1.3 * cos(radin) ,y + r / 1.3 * sin(radin), x + r / 8 * cos(radin - 150 * PI / 180),y + r / 8 * sin(radin - 150 * PI / 180), x + r / 6 * cos(radin - 180 * PI / 180), y + r / 6 * sin(radin - 180 * PI / 180), x + r / 8 * cos(radin - 210 * PI / 180),y + r / 8 * sin(radin - 210 * PI / 180) };
	ege_fillpoly(4, polypoint);
	setfillcolor(GRAY);
	ege_point polypoint1[] = { x + r / 8 * cos(radin - 150 * PI / 180),y + r / 8 * sin(radin - 150 * PI / 180),
		x + r / 6 * cos(radin - 180 * PI / 180), y + r / 6 * sin(radin - 180 * PI / 180),
		x + r / 8 * cos(radin - 210 * PI / 180),y + r / 8 * sin(radin - 210 * PI / 180),
		x + r / 6.8 * cos(radin - 210 * PI / 180),y + r / 6.8 * sin(radin - 210 * PI / 180),
		x + r / 5 * cos(radin - 180 * PI / 180), y + r / 5 * sin(radin - 180 * PI / 180),
		x + r / 6.8 * cos(radin - 150 * PI / 180),y + r / 6.8 * sin(radin - 150 * PI / 180), };
	ege_fillpoly(6, polypoint1);
}

void draw_bkVS() {
	ege_setpattern_ellipsegradient(ege_point{ 200 , 100 }, EGEACOLOR(0xFF, BLACK), 175, 175, 225, 225, EGEACOLOR(0, WHITE));
	ege_fillrect(175, 175, 225, 225);
	ege_setpattern_ellipsegradient(ege_point{ 200 , 100 }, EGEACOLOR(0xFF, GRAY), 185, 185, 215, 215, EGEACOLOR(0xFF, BLACK));
	ege_fillrect(175, 175, 225, 225);
}

void VSscrew(float CENTER_X, float CENTER_Y, float DIAL_RADIUS)//螺丝
{
	// 使用float保证计算精度（根据需求可改为double）
	const float DIAL_RADIUS_F = (float)DIAL_RADIUS;
	const float CENTER_X_F = (float)CENTER_X;
	const float CENTER_Y_F = (float)CENTER_Y;

	// 参数化定义（精确到小数点后6位）
	const float SCREW_OFFSET = DIAL_RADIUS_F * 0.055555556f; // 1/18
	const float ELLIPSE_OFFSET_X = -DIAL_RADIUS_F * 0.111111111f; // -1/9
	const float ELLIPSE_OFFSET_Y = -DIAL_RADIUS_F * 0.166666667f; // -1/6
	const float ELLIPSE_SIZE = DIAL_RADIUS_F * 0.277777778f;  // 5/18
	const float RECT_OFFSET_X = DIAL_RADIUS_F * 0.222222222f;  // 2/9
	const float RECT_OFFSET_Y = DIAL_RADIUS_F * 0.333333333f;  // 1/3
	const float RECT_WIDTH = DIAL_RADIUS_F * 0.555555556f;   // 5/9
	const float RECT_HEIGHT = DIAL_RADIUS_F * 0.611111111f;  // 11/18
	const float CROSS_OFFSET1 = DIAL_RADIUS_F * 0.055555556f;  // 1/18
	const float CROSS_OFFSET2 = DIAL_RADIUS_F * 0.027777778f;  // 1/36
	const float LINE_WIDTH = DIAL_RADIUS_F * 0.016666667f;  // 1/60

	ege_enable_aa(TRUE);

	// 绘制函数（使用浮点坐标）
	auto draw_screw = [&](float cx, float cy) {
		// 精确到小数点后6位的渐变参数
		ege_setpattern_ellipsegradient(
			ege_point{ roundf(cx), roundf(cy) }, // 最终转整数
			EGEACOLOR(0xFF, BLACK),
			roundf(cx + ELLIPSE_OFFSET_X),
			roundf(cy + ELLIPSE_OFFSET_Y),
			roundf(ELLIPSE_SIZE),
			roundf(ELLIPSE_SIZE),
			EGEACOLOR(0, WHITE)
		);
		ege_fillrect(
			roundf(cx - RECT_OFFSET_X),
			roundf(cy - RECT_OFFSET_Y),
			roundf(RECT_WIDTH),
			roundf(RECT_HEIGHT)
		);

		// 灰色渐变层（精确尺寸调整）
		const float adjusted_size = ELLIPSE_SIZE - DIAL_RADIUS_F * 0.055555556f; // 替代原-10
		ege_setpattern_ellipsegradient(
			ege_point{
					roundf(cx - SCREW_OFFSET * 0.5f),
					roundf(cy - SCREW_OFFSET * 0.5f)
			},
			EGEACOLOR(0xFF, EGERGB(170, 170, 170)),
			roundf(cx + ELLIPSE_OFFSET_X),
			roundf(cy + ELLIPSE_OFFSET_Y),
			roundf(adjusted_size),
			roundf(adjusted_size),
			EGEACOLOR(0xFF, EGERGB(20, 20, 20))
		);
		ege_fillrect(
			roundf(cx - RECT_OFFSET_X),
			roundf(cy - RECT_OFFSET_Y),
			roundf(RECT_WIDTH),
			roundf(RECT_HEIGHT)
		);

		// 十字线（亚像素级精度）
		setcolor(EGERGB(30, 30, 30));
		setlinewidth(roundf(LINE_WIDTH));

		const float cross_y = cy - CROSS_OFFSET2 * 2.0f;
		ege_line(
			roundf(cx - CROSS_OFFSET1), roundf(cross_y),
			roundf(cx + CROSS_OFFSET1), roundf(cross_y)
		);

		const float cross_x = cx;
		const float cross_y_start = cy + CROSS_OFFSET2 / 4;
		const float cross_y_end = cy - CROSS_OFFSET2 * 4.0f;
		ege_line(
			roundf(cross_x), roundf(cross_y_start),
			roundf(cross_x), roundf(cross_y_end)
		);
		};

	// 计算四个螺丝中心坐标（浮点运算）
	const float screw_radius = DIAL_RADIUS_F - SCREW_OFFSET;

	// 左上
	draw_screw(
		CENTER_X_F - screw_radius,
		CENTER_Y_F - DIAL_RADIUS_F
	);

	// 右上
	draw_screw(
		CENTER_X_F + screw_radius,
		CENTER_Y_F - DIAL_RADIUS_F
	);

	// 右下
	draw_screw(
		CENTER_X_F + screw_radius,
		CENTER_Y_F + DIAL_RADIUS_F
	);

	// 左下（新增）
	draw_screw(
		CENTER_X_F - screw_radius,
		CENTER_Y_F + DIAL_RADIUS_F
	);
}